package me.wangxuan.pat;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatLightLaf;

import me.wangxuan.pat.CustomJTable.ButtonEditor;
import me.wangxuan.pat.CustomJTable.ButtonRenderer;

public class CustomLayout extends JFrame {
	private static final long serialVersionUID = 1L;

	public CustomLayout() {
        setTitle("支付接口工具");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // Header Panel
//        JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
//        headerPanel.setBackground(Color.LIGHT_GRAY); // Set background color to differentiate
//        headerPanel.add(new JLabel("Pay Api Tool"));
//        headerPanel.setPreferredSize(new Dimension(this.getWidth(), 50)); // 设置Header高度为50
//        add(headerPanel, BorderLayout.NORTH);

        // Main Panel with Aside and Content
        JPanel mainPanel = new JPanel(new BorderLayout());
        add(mainPanel, BorderLayout.CENTER);

        // Aside Panel
        JPanel asidePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
//        asidePanel.setBackground(Color.CYAN); // Set background color to differentiate
        asidePanel.add(new JLabel("Aside"));
        asidePanel.setBorder(BorderFactory.createEtchedBorder());// 创建一个蚀刻边框
        asidePanel.setPreferredSize(new Dimension(200, this.getHeight())); // 设置asidePanel宽度为100
        mainPanel.add(asidePanel, BorderLayout.WEST);

        // Main Content Panel
        JPanel mainContentPanel = new JPanel();
//        mainContentPanel.setBackground(Color.MAGENTA); // Set background color to differentiate
        mainContentPanel.setLayout(new BoxLayout(mainContentPanel, BoxLayout.Y_AXIS));
//        mainContentPanel.add(new JLabel("Main"));
        mainContentPanel.setBorder(BorderFactory.createEtchedBorder());// 创建一个蚀刻边框
        
        JPanel row1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JComboBox<String> methodBox = new JComboBox<>(new String[]{"POST", "GET"});
        JTextField urlField = new JTextField(30);
        urlField.setText("http://www.baidu.com");
        JButton button = new JButton("Click Me");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取文本框的值
                String text = urlField.getText();
                String method = (String) methodBox.getSelectedItem();
                // 打印文本框的值或进行其他操作
                System.out.println("Text in TextField: " + text + " method:"+method);
            }
        });
        
        row1.add(methodBox);
        row1.add(urlField);
        row1.add(button);
        mainContentPanel.add(row1);
        
        
        JPanel paramsPanel = new JPanel(new BorderLayout());
        
	     // Create table model
		String[] columnNames = { "参数名", "参数值", "操作", "生成器" };
		DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
		JTable table = new JTable(tableModel);
		table.setShowGrid(true); // 显示网格线
		setupTableColumns(table);
		
		// Add button to add new rows
		JButton addButton = new JButton("Add Row");
		addButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Object[] newRow = { "", "", "生成", "Option 1" };
				tableModel.addRow(newRow);
			}
		});
				
        paramsPanel.add(new JScrollPane(table), BorderLayout.CENTER);
        paramsPanel.add(addButton, BorderLayout.SOUTH);
        mainContentPanel.add(paramsPanel);
        JPanel repsPanel = new JPanel();
        mainContentPanel.add(repsPanel);
        mainPanel.add(mainContentPanel, BorderLayout.CENTER);
        
        

        setVisible(true);
    }
	
	private void setupTableColumns(JTable table) {
		table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new JTextField()));
		table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(new JTextField()));

		// Button column
		table.getColumnModel().getColumn(2).setCellRenderer(new ButtonRenderer());
		table.getColumnModel().getColumn(2).setCellEditor(new ButtonEditor(e->{
			System.out.println(e);
		}));
//		table.getColumnModel().getColumn(2).setPreferredWidth(200);

		// Dropdown column
		JComboBox<String> comboBox = new JComboBox<>(new String[] { "时间戳", "身份证", "Option 3" });
		table.getColumnModel().getColumn(3).setCellRenderer(new DefaultTableCellRenderer());
		table.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(comboBox));
	}

    public static void main(String[] args) {
    	try {
			System.out.println(UIManager.getSystemLookAndFeelClassName());
//		    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
//		    UIManager.setLookAndFeel(FlatLightLaf.class.getName());
		    UIManager.setLookAndFeel(FlatDarkLaf.class.getName());
		} catch (Exception e) {
		    e.printStackTrace();
		}
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CustomLayout();
            }
        });
    }
}
